<?PHP
class orderEntity
{
	
	/*返回所有的订单*/
	public static function GetAllList()
	{
        return $_ENV['db']->fetchAll(" select * from `order` order by  created_time ");
	}
	
	
	/*更新价格*/
	public static function UpdatePrice($data,$oid)
	{
        UpdateTable('order', saddslashes($data),array('order_id'=>$oid));
	}
	
	public static function GetRow($oid)
	{
		$row = $_ENV['db']->fetch("SELECT * FROM `order` WHERE `order_id` = '{$oid}'");
		return $row;
	}
	
	/*[订单明细列表]*/
	public static function GetOrderDetail($oid)
	{
		return $_ENV['db']->fetchAll("SELECT * FROM `order_detail` WHERE order_id = '{$oid}' ");
	}

	/*[订单明细列表]*/
	public static function GetOrderDetails($oid)
	{
		return $_ENV['db']->fetchAll("SELECT o.*,p.num,p.code,p.price as curprice FROM `order_detail` as o LEFT JOIN `product` as p ON o.product_id=p.product_id WHERE `order_id` = '{$oid}'");
	}
	
	/*订单操作历史记录*/
	public static function GetOrderHistorys($order_sn)
	{
		return $_ENV['db']->fetchAll("SELECT * FROM `order_history` WHERE `order_sn` = '{$order_sn}'");
	}
	/*订单历史记录*/
	public static function GetOrderPayedHistory($oid)
	{
		return $_ENV['db']->fetchAll("SELECT * FROM `order_payed_history` WHERE `order_id` = '{$oid}'");
	}

	/*[新增主单]*/
	public static function AddOrder($osn,$customer,$pprice,$uid,$audit_member_id)
	{
		/*[新增订单主表]*/
			$data = array(
				'order_sn'=>$osn,                                  // 订单流水号 系统订单号
				'customer_id'  => $customer['customer_id'],        // 客户ID
            	'customer_name'=> $customer['customer_name'],	   // 客户名称
				'audit_state' => '1',                              // 待确认
				'price' => '0',                                    // 原价格
				'pprice' => $pprice,                               // 实际价格
				'created_time' => $_ENV['time'],
                'last_time' => $_ENV['time'],
				'co_member_id' =>$uid,               			   // 所属业务员ID
				'audit3_uid' =>$audit_member_id,               	   // 审核用户ID
				);
			 $oid = InsertTable('order', saddslashes($data));
			 return $oid;
	}
	
	/*[新增合同信息]*/
	public static function AddContract($oid,$contract,$contract_number,$pc,$rece_id,$fid)
	{
		/*[新增合同信息]*/
			$data = array(
				'order_id' => $oid,                                 // 订单号
				'contract' => $contract ,                           // 合同类型
            	'contract_number'=> $contract_number,	            // 合同号
				'pc' => $pc,                               			// 合同信息录入
				'rece_id' => $rece_id,                             	// 收货地址
				'fid' => $fid,                                		// 发票ID
				);
			 $con_id = InsertTable('order_contract', saddslashes($data));
			 return $con_id;
	}
	
	
	public static function IsShenPi($uid,$oid)
	{
		$row = $_ENV['db']->fetchCol("SELECT count(1) FROM order_audit_history WHERE `uid` = '{$uid}' and `order_id` = '{$oid}'");
		if(!$row)
		{
			return false;
		}
		return true;
	}
	
	/*[返回已完结、全额已支付的订单列表]*/
	public static function GetOrderList()
	{
		return $_ENV['db']->fetchAll(" select * from `order` where audit_state='6' and istuihuo='0' ");
	}
	
	/*[订单汇总]*/
	public static function GetOrderSummary($where)
	{
		$strsql = 'select customer_id,customer_name,count(o.order_id) as countorder,sum(d.total_price) as total_price,sum(d.total_pprice) as total_pprice,sum(payed) as pays,(sum(d.total_pprice)- sum(payed)) as qians from `order` o left join (select order_id,sum(product_total_price) as total_price,sum(product_total_pprice) as total_pprice from `order_detail`  where '.$where[1].' group by  order_id ) d on o.order_id=d.order_id where '.$where[0].' group by customer_name';
		return $_ENV['db']->fetchAll($strsql);
		
	}
	/*[订单列表]*/
	public static function GetOrders($where)
	{
		$sql = "select o.customer_id,o.order_sn,o.order_id,o.created_time,o.audit_state,d.totoal_price,d.total_ppirce,o.payed, (d.total_ppirce -o.payed) qian from `order` o left join (select order_id, sum(product_total_price) as totoal_price,sum(product_total_pprice) as total_ppirce from  `order_detail`  where ".$where[1]."  group by  order_id ) d on  o.order_id=d.order_id where ".$where[0];      
		return $_ENV['db']->fetchAll($sql);
	}
}
?>